import { computed } from 'vue'
import type { ComputedRef } from 'vue'
import { getAttrs } from 'generator/utils'

export function useTempValue(attrs: ComputedRef<any>, comp: TComp, panelJson: TPanel) {
  return computed(() => {
    const defauleValue = getDefaultValue(comp.key)
    if(comp.parentId) {
      const parentComp = panelJson.children.find(item => item.id == comp.parentId)!
      const parentAttrs = getAttrs(parentComp.attrs)
      const filed = parentAttrs.vFor?.open ? parentAttrs.vFor?.variable : parentAttrs.vModel
      const parentVModelData = panelJson.data.find(item => item.variable == filed)
      const modelData = Array.isArray(parentVModelData?.value) ? parentVModelData?.value?.[0] : parentVModelData?.value
      return modelData?.[attrs.value.vModel] ?? defauleValue
    }
    if(!attrs.value.vModel) return defauleValue
    const filed = attrs.value.vFor?.open ? attrs.value.vFor?.variable : attrs.value.vModel
    const vModelData = panelJson.data.find(item => item.variable == filed)
    let modelData: any;
    if(attrs.value.vFor?.open) {
      modelData = Array.isArray(vModelData?.value) ? vModelData?.value?.[0]?.[attrs.value.vModel] : vModelData?.value
    } else {
      modelData = vModelData?.value
    }
    return modelData ?? defauleValue
  })
}

function getDefaultValue(key: string) {
  const map = <{ [k: string]: any }>{
    text: '这是一段文字',
    image: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
    noticeBar: 'uView UI众多组件覆盖开发过程的各个需求，组件功能丰富，多端兼容。让您快速集成，开箱即用',
    grid: [{id: '1.1',text: '文字',icon: 'photo-o'},{id: '1.2',text: '文字',icon: 'shopping-cart-o'},{id: '1.3',text: '文字',icon: 'cart-o'},{id: '1.4',text: '文字',icon: 'coupon-o'},{id: '1.5',text: '文字',icon: 'fire-o'},{id: '1.6',text: '文字',icon: 'setting-o'}],
    input: '',
    swiper: [{ url: 'https://cdn.uviewui.com/uview/swiper/swiper2.png', type: 'image' }, { url: 'https://cdn.uviewui.com/uview/swiper/swiper3.png', type: 'image' }, { url: 'https://cdn.uviewui.com/uview/resources/video.mp4', type: 'video' }],
    divider: '分割线',
    date: ['2024', '09', '25'],
    rate: 3,
    search: '',
    numberBox: 0,
    textarea: '',
    checkbox: ['0', '1'],
    radio: '0',
    switch: '0',
    slider: 30,
    badge: 20,
    tag: '标签'
  }
  return map[key]
}